﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.Application.Contracts.DomainEntities.ContractManagement;
using SOEI.Solution.DomainEntities.ContractManagement.DomainService;
using SOEI.Solution.DomainEntities.ContractManagement.Dtos;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace SOEI.Solution.DomainEntities.ContractManagement
{
    /// <summary>
    /// 【扩展模块】  <br/>
    /// 【功能描述】  ：XXX 模块<br/>
    /// 【创建日期】  ：<br/>
    /// 【开发人员】  ：<br/>
    ///</summary>
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = false)]
    public class ContractAppService : SolutionAppServiceBase, IContractAppService
    {
        /// <summary>
        ///【ContractSummary】领域服务
        /// </summary>
        private readonly IContractSummaryManager _contractSummaryManager;

        /// <summary>
        ///【ContractBody】领域服务
        /// </summary>
        private readonly IContractBodyManager _contractBodyManager;

        /// <summary>
        ///【ContractStatus】领域服务
        /// </summary>
        private readonly IContractStatusManager _contractStatusManager;

        public ContractAppService(
            IContractSummaryManager contractSummaryManager,
            IContractBodyManager contractBodyManager,
            IContractStatusManager contractStatusManager
        )
        {
            _contractSummaryManager = contractSummaryManager;
            _contractBodyManager = contractBodyManager;
            _contractStatusManager = contractStatusManager;
        }


        #region -------------------------------------------------辅助工具生成----------------------------------------------

        /// <summary>
        ///【交易合同】获取的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(/*ContractPermissions.Node*/)]
        public async Task<PagedResultDto<ContractListDto>> GetPaged(GetContractsInput input)
        {
            var query = _contractSummaryManager
                .QueryAsNoTracking()
                .Include(zy => zy.ContractStatus);

            query
                //模糊搜索 字段Id
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Id.Contains(input.FilterText))
                //模糊搜索 字段ContractRegistrationId
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ContractRegistrationId.Contains(input.FilterText))
                //模糊搜索 字段Transactor
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Transactor.Contains(input.FilterText))
                //模糊搜索 字段ContractStatusId
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ContractStatusId.Contains(input.FilterText))
                //模糊搜索 字段HouseId
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.HouseId.Contains(input.FilterText))
                //模糊搜索 字段AgentIdNumber
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.AgentIdNumber.Contains(input.FilterText))
                //模糊搜索 字段AgentName
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.AgentName.Contains(input.FilterText))
                //模糊搜索 字段ContractTypeCode
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ContractTypeCode.Contains(input.FilterText))
                //模糊搜索 字段CertificateName
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CertificateName.Contains(input.FilterText))
                //模糊搜索 字段CertificateCode
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CertificateCode.Contains(input.FilterText))
                //模糊搜索 字段BrokerageInstitutionId
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.BrokerageInstitutionId.Contains(input.FilterText))
                //模糊搜索 字段BrokerageInstitutionName
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.BrokerageInstitutionName.Contains(input.FilterText))
                //模糊搜索 字段PartyAPassword
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.PartyAPassword.Contains(input.FilterText))
                //模糊搜索 字段PartyBPassword
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.PartyBPassword.Contains(input.FilterText))
                //模糊搜索 字段AttentionLine
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.AttentionLine.Contains(input.FilterText))
                //模糊搜索 字段HouseUsage
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.HouseUsage.Contains(input.FilterText))
                //模糊搜索 字段HouseProperty
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.HouseProperty.Contains(input.FilterText))
                //模糊搜索 字段SystemPlatform
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SystemPlatform.Contains(input.FilterText))
                //模糊搜索 字段CertificateName_2
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CertificateName_2.Contains(input.FilterText))
                //模糊搜索 字段EntrustmentContractId
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.EntrustmentContractId.Contains(input.FilterText))
                //模糊搜索 字段ContractHandlingStatusCode
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ContractHandlingStatusCode.Contains(input.FilterText))
                //模糊搜索 字段LocatedInformation
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.LocatedInformation.Contains(input.FilterText))
                //模糊搜索 字段PaymentMethodCode
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.PaymentMethodCode.Contains(input.FilterText))
                //模糊搜索 字段HTYXQ
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.HTYXQ.Contains(input.FilterText))
                //模糊搜索 字段ContractModelConfigCode
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ContractModelConfigCode.Contains(input.FilterText))
                //模糊搜索 字段ContractIdentification
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ContractIdentification.Contains(input.FilterText))
                //模糊搜索 字段Seller
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Seller.Contains(input.FilterText))
                //模糊搜索 字段Buyer
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Buyer.Contains(input.FilterText))
                //模糊搜索 字段SellerIdCardNumber
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SellerIdCardNumber.Contains(input.FilterText))
                //模糊搜索 字段BuyerIdCardNumber
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.BuyerIdCardNumber.Contains(input.FilterText))
                //模糊搜索 字段IsMortgage
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.IsMortgage.Contains(input.FilterText))
                //模糊搜索 字段SBBZ
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SBBZ.Contains(input.FilterText))
                //模糊搜索 字段IsNeedRecheck
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.IsNeedRecheck.Contains(input.FilterText))
                //模糊搜索 字段RecheckOrganizationId
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.RecheckOrganizationId.Contains(input.FilterText))
                //模糊搜索 字段RecheckOrganizationName
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.RecheckOrganizationName.Contains(input.FilterText))
                //模糊搜索 字段RecheckPeopleId
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.RecheckPeopleId.Contains(input.FilterText))
                //模糊搜索 字段RecheckPeopleName
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.RecheckPeopleName.Contains(input.FilterText))
                //模糊搜索 字段ContractOwnerOrganizationId
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ContractOwnerOrganizationId.Contains(input.FilterText))
                //模糊搜索 字段ImmovablesWarrantIdCode
                .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ImmovablesWarrantIdCode.Contains(input.FilterText))
            ;

            // TODO:根据传入的参数添加过滤条件

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<ContractListDto>>(entityList);

            return new PagedResultDto<ContractListDto>(count, entityListDtos);
        }

        /// <summary>
        ///【交易合同】通过指定id获取MemberListDto信息
        /// </summary>
        [AbpAuthorize()]
        public async Task<ContractListDto> GetById(EntityDto<string> input)
        {
            var entity = await _contractSummaryManager.FindByIdAsync(input.Id);
            var dto = ObjectMapper.Map<ContractListDto>(entity);

            return dto;
        }

        /// <summary>
        ///【交易合同】 获取编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetContractForEditOutput> GetForEdit(EntityDto<string> input)
        {
            var output = new GetContractForEditOutput();
            output.Contract = new ContractEditDto();

            if (!input.Id.IsNullOrWhiteSpace())
            {
                var entity = await _contractSummaryManager.FindByIdAsync(input.Id);
                output.Contract = ObjectMapper.Map<ContractEditDto>(entity);
                output.Contract.ContractBodys = await _contractBodyManager.FindByContractSummaryIdAsync(input.Id);
            }

            return output;
        }

        /// <summary>
        ///【交易合同】 添加或者修改的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task CreateOrUpdate(CreateOrUpdateContractInput input)
        {
            if (!input.Contract.Id.IsNullOrWhiteSpace())
            {
                await Update(input.Contract);
            }
            else
            {
                await Create(input.Contract);
            }
        }
        /// <summary>
        ///【交易合同】新增
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task<ContractEditDto> Create(ContractEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<ContractSummary>(input);
            //调用领域服务
            entity = await _contractSummaryManager.CreateAsync(entity);

            var dto = ObjectMapper.Map<ContractEditDto>(entity);
            return dto;
        }

        /// <summary>
        ///【交易合同】编辑
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task Update(ContractEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新
            var key = input.Id;
            var entity = await _contractSummaryManager.FindByIdAsync(key);
            //  input.MapTo(entity);
            //将input属性的值赋值到entity中
            ObjectMapper.Map(input, entity);
            await _contractSummaryManager.UpdateAsync(entity);
        }

        /// <summary>
        ///【交易合同】删除信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task Delete(EntityDto<string> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _contractSummaryManager.DeleteAsync(input.Id);
        }

        /// <summary>
        ///【交易合同】 批量删除Member的方法
        /// </summary>
        [AbpAuthorize()]
        public async Task BatchDelete(ICollection<string> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _contractSummaryManager.BatchDeleteAsync(input);
        }

        #endregion

        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展应用服务实现*/




        #endregion
    }
}
